import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * date 2021/5/31 8:14
 */
@SuppressWarnings(value = "unused")
public class 股票 extends Subject{

    private double oldPrice = 10;
    private double price = 10;

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObserver() {
        if ((price - oldPrice) / oldPrice > 0.05) {
            for (Observer observer : observers) {
                observer.update();
            }
        }
    }

    private final List<Observer> observers = new ArrayList<>();

    public double getPrice() {
        return price;
    }

    public 股票 setPrice(double price) {
        this.oldPrice = this.price;
        this.price = price;
        return this;
    }
}
